POJ1573 ZOJ1708 UVA10116 UVALive5334 HDU1035 Robot Motion【模拟】

96 篇文章 5 订阅
86 篇文章 9 订阅

Robot Motion
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16166 Accepted: 7643
Description
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0K1yHoGJ-1614299697840)(http://poj.org/images/1573_1.jpg)]

A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are

N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)

For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.

Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.

You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.

Input

There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.

Output

For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word “step” is always immediately followed by “(s)” whether or not the number before it is 1.

Sample Input

3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0

Sample Output

10 step(s) to exit
3 step(s) before a loop of 8 step(s)
Source

Mid-Central USA 1999

Regionals 1999 >> North America - Mid-Central USA

问题链接POJ1573 ZOJ1708 UVA10116 UVALive5334 HDU1035 Robot Motion
问题简述:(略)
问题分析
    有r*c区域,机器人从第一行的某列进入,该区域全部由’N’ , ‘S’ , ‘W’ , ‘E’ ,走到某个区域的时候只能按照该区域指定的方向进行下一步,计算机器人能否走出该片区域,若不能则输出开始绕圈的步数和圈的大小。
    这个问题有2种解法,DFS和BFS都可以。用BFS来解时,需要用STL的队列来辅助。
之前的代码过于繁琐,另外写了一个解题程序。用模拟来实现,模拟走迷宫直到走出迷宫。用数组mcnt[][]记住走了几步就可以了。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ1573  ZOJ1708 UVA10116 UVALive5334 Robot Motion */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 10;
char maze[N][N + 1];
int mcnt[N][N];

int main()
{
    int r, c, r2, c2;
    while(scanf("%d%d%d", &r, &c, &c2) && (r || c || c2)) {
        for(int i = 0; i < r; i++)
            scanf("%s", maze[i]);

        memset(mcnt, 0, sizeof(mcnt));
        int cnt = 1;
        r2 = 0, c2--;
        while(mcnt[r2][c2] == 0 && r2 >= 0 && r2 < r && c2 >= 0 && c2 < c) {
            mcnt[r2][c2] = cnt++;
            if(maze[r2][c2] == 'N') r2--;
            else if(maze[r2][c2] == 'S') r2++;
            else if(maze[r2][c2] == 'W') c2--;
            else if(maze[r2][c2] == 'E') c2++;
        }

        if(r2 >= 0 && r2 < r && c2 >= 0 && c2 < c)
            printf("%d step(s) before a loop of %d step(s)\n", mcnt[r2][c2] - 1, cnt - mcnt[r2][c2]);
        else printf("%d step(s) to exit\n", cnt - 1);
    }

    return 0;
}

AC的C语言程序(HDU以外,DFS)如下:

/* POJ1573  ZOJ1708 UVA10116 UVALive5334 Robot Motion */

#include <stdio.h>
#include <string.h>

#define N 10
char maze[N][N + 1];
int vis[N][N], step[N][N], r, c, cnt;

void dfs(int row, int col)
{
    cnt++;
    if(row < 0 || row >= r || col < 0 || col >= c)
                printf("%d step(s) to exit\n", cnt);
    else if(vis[row][col])
        printf("%d step(s) before a loop of %d step(s)\n", step[row][col], cnt - step[row][col]);
    else {
        vis[row][col] = 1;
        step[row][col] = cnt;
        if(maze[row][col] == 'S') dfs(row + 1, col);
        if(maze[row][col] == 'N') dfs(row - 1, col);
        if(maze[row][col] == 'E') dfs(row, col + 1);
        if(maze[row][col] == 'W') dfs(row, col - 1);
    }
}

int main(void)
{
    int enter, i;
    while(scanf("%d%d%d", &r, &c, &enter) && (r || c || enter)) {
        for(i = 0; i < r; i++)
            scanf("%s", maze[i]);

        memset(vis, 0, sizeof(vis));
        cnt = -1;
        dfs(0, enter - 1);
    }

    return 0;
}

AC的C语言程序(HDU,DFS)如下:

/* HDU1035 Robot Motion */

#include <stdio.h>
#include <string.h>

#define N 10
char maze[N][N + 1];
int vis[N][N], step[N][N], r, c, cnt;

void dfs(int row, int col)
{
    cnt++;
    if(row < 0 || row >= r || col < 0 || col >= c)
                printf("%d step(s) to exit\n", cnt);
    else if(vis[row][col])
        printf("%d step(s) before a loop of %d step(s)\n", step[row][col], cnt - step[row][col]);
    else {
        vis[row][col] = 1;
        step[row][col] = cnt;
        if(maze[row][col] == 'S') dfs(row + 1, col);
        if(maze[row][col] == 'N') dfs(row - 1, col);
        if(maze[row][col] == 'E') dfs(row, col + 1);
        if(maze[row][col] == 'W') dfs(row, col - 1);
    }
}

int main(void)
{
    int enter, i;
    while(scanf("%d%d", &r, &c) && (r || c)) {
        scanf("%d", &enter);
        for(i = 0; i < r; i++)
            scanf("%s", maze[i]);

        memset(vis, 0, sizeof(vis));
        cnt = -1;
        dfs(0, enter - 1);
    }

    return 0;
}

AC的C++语言程序(HDU以外,BFS)如下:

/* POJ1573  ZOJ1708 UVA10116 UVALive5334 Robot Motion */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int N = 10;
char maze[N][N + 1];
bool vis[N][N];
int step[N][N], r, c, enter, cnt;

struct Node {
    int row, col;
    Node(int r = 0, int c = 0) : row(r), col(c) {}
};

void bfs()
{
    memset(vis, false, sizeof(vis));
    step[0][enter - 1] = 0;
    queue<Node> q;
    q.push(Node(0, enter - 1));
    int cnt = -1;
    while(!q.empty()) {
        Node t = q.front();
        q.pop();
        cnt++;
        if(t.row < 0 || t.row >= r || t.col < 0 || t.col >= c)
                    printf("%d step(s) to exit\n", cnt);
        else if(vis[t.row][t.col])
            printf("%d step(s) before a loop of %d step(s)\n", step[t.row][t.col], cnt - step[t.row][t.col]);
        else {
            vis[t.row][t.col] = true;
            step[t.row][t.col] = cnt;
            if(maze[t.row][t.col] == 'S') q.push(Node(t.row + 1, t.col));
            if(maze[t.row][t.col] == 'N') q.push(Node(t.row - 1, t.col));
            if(maze[t.row][t.col] == 'E') q.push(Node(t.row, t.col + 1));
            if(maze[t.row][t.col] == 'W') q.push(Node(t.row, t.col - 1));
        }
    }
}

int main()
{
    while(scanf("%d%d%d", &r, &c, &enter) && (r || c || enter)) {
        for(int i = 0; i < r; i++)
            scanf("%s", maze[i]);

        bfs();
    }

    return 0;
}

AC的C++语言程序(HDU,BFS)如下:

/* HDU1035 Robot Motion */

#include <bits/stdc++.h>

using namespace std;

const int N = 10;
char maze[N][N + 1];
bool vis[N][N];
int step[N][N], r, c, enter, cnt;

struct Node {
    int row, col;
    Node(int r = 0, int c = 0) : row(r), col(c) {}
};

void bfs()
{
    memset(vis, false, sizeof(vis));
    step[0][enter - 1] = 0;
    queue<Node> q;
    q.push(Node(0, enter - 1));
    int cnt = -1;
    while(!q.empty()) {
        Node t = q.front();
        q.pop();
        cnt++;
        if(t.row < 0 || t.row >= r || t.col < 0 || t.col >= c)
                    printf("%d step(s) to exit\n", cnt);
        else if(vis[t.row][t.col])
            printf("%d step(s) before a loop of %d step(s)\n", step[t.row][t.col], cnt - step[t.row][t.col]);
        else {
            vis[t.row][t.col] = true;
            step[t.row][t.col] = cnt;
            if(maze[t.row][t.col] == 'S') q.push(Node(t.row + 1, t.col));
            if(maze[t.row][t.col] == 'N') q.push(Node(t.row - 1, t.col));
            if(maze[t.row][t.col] == 'E') q.push(Node(t.row, t.col + 1));
            if(maze[t.row][t.col] == 'W') q.push(Node(t.row, t.col - 1));
        }
    }
}

int main()
{
    while(scanf("%d%d", &r, &c) && (r || c)) {
        scanf("%d", &enter);
        for(int i = 0; i < r; i++)
            scanf("%s", maze[i]);

        bfs();
    }

    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值